package com.codingfuture.erp_system.web.controller;

import com.codingfuture.erp_system.dto.AddReturnOrdersDto;
import com.codingfuture.erp_system.dto.PageData;
import com.codingfuture.erp_system.dto.ReturnOrdersDto;
import com.codingfuture.erp_system.entity.*;
import com.codingfuture.erp_system.service.OrdersService;
import com.codingfuture.erp_system.service.ReturnOrdersService;
import com.codingfuture.erp_system.util.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v2")
public class ReturnOrdersController {
    @Autowired
    private ReturnOrdersService returnOrdersService;
    @Autowired
    private OrdersService ordersService;

    /**
     * 分页查询/模糊查询
     */
    @GetMapping("/returnorders/listByPage")
    public Result findByAll(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int rows, ReturnOrders returnOrders) {
        Page<ReturnOrders> page1 = PageHelper.startPage(page, rows);
        List<ReturnOrders> returnOrdersList = returnOrdersService.findByAll(returnOrders);
        for (ReturnOrders returnOrdersDto : returnOrdersList) {
            List<ReturnOrderDetailList> list1 = returnOrdersService.findReturnOrdersDto(returnOrdersDto.getUuid());
            returnOrdersDto.setReturnOrderDetailList(list1);
        }
        PageData<ReturnOrders> data = new PageData<>(returnOrdersList, page1.getTotal());
        return Result.ok(data);
    }

    /**
     * 采购退货查询--退货出库查询
     */
    @GetMapping("/returnorders/salesReturnlistByPage")
    public Result salesReturnlistByPage(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int rows) {
        Page<ReturnOrders> page1 = PageHelper.startPage(page, rows);
        List<ReturnOrders> returnOrdersList = returnOrdersService.salesReturnlistByPage();
        for (ReturnOrders orders : returnOrdersList) {
            List<ReturnOrderDetailList> detail = returnOrdersService.findReturnOrderDetail(orders.getUuid());
            orders.setReturnOrderDetailList(detail);
        }
        PageData<ReturnOrders> data = new PageData<>(returnOrdersList, page1.getTotal());
        return Result.ok(data);
    }

    @GetMapping("/returnorders/selectStore")
    public Result selectStore() {
        List<Store> list = returnOrdersService.selectStore();
        return Result.OK(list);
    }

    @PostMapping("/returnorders/doCheck")
    public Result returnDoCheck(Integer id) {
        Date date = new Date();
        Boolean result = returnOrdersService.returnDoCheck(id, date);
        return result ? Result.ok() : Result.error(201, "错误");
    }

    @PostMapping("/returnorders/doInStore")
    public Result doInStore(Integer id, Integer storeuuid) {
        Date date = new Date();
        return returnOrdersService.doInStore(id, date, storeuuid) ? Result.ok() : Result.error(201, "错误");
    }

    @PostMapping("/returnorders/add")
    public Result add(@RequestBody AddReturnOrdersDto addReturnOrdersDto, HttpSession session) {
        Date date = new Date();
        ReturnOrdersDto ordersDto = new ReturnOrdersDto();
        List<ReturnOrdersDetail> returnOrderDetails = addReturnOrdersDto.getReturnOrderDetails();
        Double totalMoney = 0.0;
        Object name = session.getAttribute("name");
        Role role = ordersService.findByName(name);
        Integer uuid = role.getUuid();
        Integer supplieruuid = addReturnOrdersDto.getSupplieruuid();

        for (ReturnOrdersDetail returnOrdersDto : returnOrderDetails) {
            totalMoney += returnOrdersDto.getMoney();
        }
        ordersDto.setCreatetime(date);
        ordersDto.setSupplieruuid(supplieruuid);
        ordersDto.setTotalmoney(totalMoney);
        ordersDto.setCreater(uuid);
        int num = returnOrdersService.addByOrdersDto(ordersDto);
        long dtoUuid = ordersDto.getUuid();
        for (ReturnOrdersDetail returnOrderDetail : returnOrderDetails) {
            returnOrderDetail.setReturnordersuuid((int) dtoUuid);
            returnOrderDetail.setEndtime(date);
        }
        int i = returnOrdersService.addByReturnOrderDetails(returnOrderDetails);
        if (num != 0 && i != 0) {
            return Result.ok();
        } else {
            return Result.error(500, "添加失败");
        }

    }
}
